package com.kh.pajx.sop.base;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.kh.pajx.sop.domain.SOPCurrentUser;
import com.kh.pajx.sop.service.AreaMangerBackService;
import com.kh.pajx.sop.service.BalanceAppService;
import com.kh.pajx.sop.service.ForeignBackService;
import com.kh.pajx.sop.service.IHsEquOnlineService;
import com.kh.pajx.sop.service.IHsEquSwitchService;
import com.kh.pajx.sop.service.IHsEquipmentInfoService;
import com.kh.pajx.sop.service.IHsExpressService;
import com.kh.pajx.sop.service.IHsNoticeService;
import com.kh.pajx.sop.service.IHsSchoolRelationService;
import com.kh.pajx.sop.service.ISchoolService;
import com.kh.pajx.sop.service.ISopLoginService;
import com.kh.pajx.sop.service.IStockDevInDetailService;
import com.kh.pajx.sop.service.IStockDevInService;
import com.kh.pajx.sop.service.IStockDevOutDetailService;
import com.kh.pajx.sop.service.IStockDevOutService;
import com.kh.pajx.sop.service.IStockDevService;
import com.kh.pajx.sop.service.IStockDeviceDicService;
import com.kh.pajx.sop.service.ISupplierService;
import com.kh.pajx.sop.service.ISysLogService;
import com.kh.pajx.sop.service.ISysMenuAuthService;
import com.kh.pajx.sop.service.ISysSopDebugService;
import com.kh.pajx.sop.service.ISysUserGroupService;
import com.kh.pajx.sop.service.ISysUserRoleService;
import com.kh.pajx.sop.service.order.IReplaceCardOrderDetailService;
import com.kh.pajx.sop.service.order.IReplaceCardOrderPhotoService;
import com.kh.pajx.sop.service.order.IReplaceCardOrderRelationService;
import com.kh.pajx.sop.service.order.IReplaceCardOrderService;
import com.kh.pajx.sop.service.school.IBossEcinfoService;
import com.kh.pajx.sop.service.school.IHsSchoolInfoService;
import com.kh.pajx.sop.service.sys.ISysAreaService;
import com.kh.pajx.sop.service.sys.ISysDepartmentService;
import com.kh.pajx.sop.service.sys.ISysDeptAreaService;
import com.kh.pajx.sop.service.sys.ISysDictionaryService;
import com.kh.pajx.sop.service.user.ISysInsideUserService;
import com.kh.pajx.sop.service.user.ISysOutsideUserService;
import com.kh.pajx.sop.util.web.CSRFTokenManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Struts2 Action控制器基类
 * @author:	azzcsimp
 * @Createdate:	2014年6月19日 下午3:44:22
 */
public class BaseAction extends ActionSupport {
	
	private static final long serialVersionUID = 4023271929252960966L;

	// ====================Service实例声明========================
	// ==> 系统设置
	@Resource
	protected ISysDictionaryService sysDictionaryService;
	@Resource
	protected ISysAreaService sysAreaService;
	@Resource
	protected ISopLoginService sopLoginService;
	@Resource
	protected ISysLogService sysLogService;
	@Resource
	protected ISysDepartmentService sysDepartmentService;
	@Resource
	protected IBossEcinfoService bossEcinfoService;
	@Resource
	protected ISysDeptAreaService sysDeptAreaService;
	
	// ==> 权限菜单
	@Resource
	protected ISysUserGroupService sysUserGroupService;
	@Resource
	protected ISysUserRoleService sysUserRoleService;
	@Resource
	protected ISysMenuAuthService sysMenuAuthService;
	
	
	// ==> 用户信息
	@Resource
	protected ISysInsideUserService sysInsideUserService;
	@Resource
	protected ISysOutsideUserService sysOutsideUserService;
	
	// ==> 学校
	@Resource
	protected ISchoolService schoolService;
	@Resource
	protected IHsSchoolInfoService hsSchoolInfoService;
	@Resource
	protected IHsSchoolRelationService hsSchoolRelationService;
	
	
	// ==> 设备
	@Resource   
	protected IHsEquipmentInfoService hsEquipmentInfoService;
	@Resource
	protected IHsEquSwitchService hsEquSwitchService;
	@Resource
	protected IHsEquOnlineService hsEquOnlineService;
	
	//库存管理
	@Resource
	protected IStockDeviceDicService stockService;
	@Resource
	protected IStockDevInService devInService;
	@Resource
	protected IStockDevInDetailService detailSevice;
	@Resource
	protected IStockDevOutService outService;
	@Resource
	protected IStockDevOutDetailService outDetailService;
	@Resource
	protected IStockDevService devService;
	@Resource
	protected ISysSopDebugService sysSopDebugService;
	@Resource
	protected ISupplierService supplierService;
	
	//对外结算
	@Resource
	protected  ForeignBackService foreignService;
	@Resource
	protected  BalanceAppService balanceAppService;
	@Resource
	protected  AreaMangerBackService areaMangerService;
	
	// 补卡单据
	@Resource
	protected IReplaceCardOrderService replaceCardOrderService;
	@Resource
	protected IReplaceCardOrderDetailService replaceCardOrderDetailService;
	@Resource
	protected IReplaceCardOrderPhotoService replaceCardOrderPhotoService;
	@Resource
	protected IReplaceCardOrderRelationService replaceCardOrderRelationService;
	
	//公告管理
	@Resource
	protected IHsNoticeService hsNoticeService;
	@Resource
	protected IHsExpressService hsExpressService;
	protected static final String NOTOKEN = "noToken"; 
	
	
	// ====================用户信息获取============================
	/**
	 * 获取当前登录用户
	 * @return   当前登录用户
	 * @author:	azzcsimp
	 * @Createdate:	2014年7月2日 下午3:49:33
	 */
	protected SOPCurrentUser getCurrentUser(){
		return (SOPCurrentUser)ActionContext.getContext().getSession().get("currentUser");
	}
	
	/**
	 * 存储CSRF防护随机因子
	 * @author:	azzcsimp
	 * @Createdate:	2014年12月18日 上午10:31:58
	 */
	protected void setCSRFToken(){
		ActionContext.getContext().put("csrfToken", CSRFTokenManager.getTokenForSession(ServletActionContext.getRequest().getSession()));
	}
	
	protected String getCSRFToken(){
		return ServletActionContext.getRequest().getSession().getAttribute(CSRFTokenManager.CSRF_TOKEN_FOR_SESSION_ATTR_NAME).toString();
	}

	/**
	 * 校验CSRF防护随机因子
	 * @return   
	 * @author:	azzcsimp
	 * @Createdate:	2014年12月18日 上午10:38:33
	 */
	protected boolean checkCSRFToken(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String CSRFToken = request.getParameter("CSRFToken");
		if(CSRFToken == null || !CSRFToken.equals(request.getSession().getAttribute(CSRFTokenManager.CSRF_TOKEN_FOR_SESSION_ATTR_NAME).toString())){
	         return false;
		}else{
			return true;
		} 
	}
	
	/**
	 * 校验AJAX交互CSRF防护随机因子
	 * @return   
	 * @author:	azzcsimp
	 * @Createdate:	2014年12月19日 下午6:00:30
	 */
	protected boolean isValidCsrfHeaderToken() {
		HttpServletRequest request = ServletActionContext.getRequest();
        if (request.getHeader("__RequestVerificationToken") == null
                || request.getSession()
                        .getAttribute(CSRFTokenManager.CSRF_TOKEN_FOR_SESSION_ATTR_NAME) == null
                || !request
                        .getHeader("__RequestVerificationToken")
                        .equals(request.getSession()
                                .getAttribute(
                                        CSRFTokenManager.CSRF_TOKEN_FOR_SESSION_ATTR_NAME)
                                .toString())) {
            return false;
        }
        return true;
    }

	// ====================分页参数================================

	/** 当前页 */
	protected int pageNum = 1;

	/** 每页显示多少条记录 
	 * 如使用dwz做分页请使用numPerPage代替
	 * */
	@Deprecated
	protected int pageSize = 10;

	/** 每页显示多少条记录 兼容dwz */
	protected int numPerPage = 20;

	/** 查询排序字段 */
	protected String orderField;

	/** 查询排序规则 */
	protected String orderDirection;

	// ========================set/get==============================

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderDirection() {
		if(StringUtils.isNotEmpty(orderDirection)){
			if(orderDirection.equalsIgnoreCase("desc")){
				return "desc";
			}
		}
		return "asc";
	}

	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

}
